var tools = require("../../common/Tools.js").tools; 
var cache=require("../../common/Cache.js").cache;
var dataFilter = require("../../common/DataFilter.js").dataFilter;
// 首页
const app = getApp();
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    clientHeight:'',
    imageList:[],
    // 当前银行广告轮播图索引
    advertisingIndex: 0,
    contact:null,
    DataList:null,
    tipmore: app.maImgburl.webUrl+"/image-more.png",
    needfile: app.maImgburl.webUrl +"/icon-file.png",
    referral: app.maImgburl.webUrl +"/icon-referral.png",
    ContactUs: app.maImgburl.webUrl +"/icon-phone.png",
    apply: app.maImgburl.webUrl +"/icon-apply.png",
    imagetel: app.maImgburl.webUrl +"/image-tel.png",
    imagephone: app.maImgburl.webUrl +"/image-phone.png"
  },

  
  // 轮播图切换时，获取当前显示轮播图索引值
  advertisingChange (event) {
    // console.log(event.detail.current);
    this.setData({
      advertisingIndex: event.detail.current
    })
  },
  // 跳转到对应的银行广告详情页面
  // goToDetail (e) {
  //   // var ID = e.currentTarget.dataset.ID;
  //   var ID = e.currentTarget.dataset.code;
  //   wx.navigateTo({
  //     url: `/pages/BankAdvertisingDetail/BankAdvertisingDetail?id=${ID}`,
  //   })
  // },

  getCarouselList(){
    var that=this
    var c_newList = this.selectComponent("#newList");
    wx.request({
      //url: app.macauWebApiList.api_Article(app.macauweburl.webApiUrl,'70'),
      // url: "http://10.68.2.9/CenMacauCMS2/api/AppArticle/GetBankArticleCarousel",
      url: app.macauweburl.webApiUrl+"AppArticle/GetBankArticleCarousel",
      success: function (result) {
        //cache.clear();
        if (result == undefined && result == null) {
          return;
        }
        for(var o of result.data){
          o.littleImg = o.Path;
        }

        // cache.set("allArticle", result.data.InnerList);
        // mzh1804661
        // var filterArticle = dataFilter.getFilter("Type", "EX", result.data.InnerList, 3);

        // for (var i = 0; i < filterArticle.length;i++){
        //   filterArticle[i].Path = filterArticle[i].Images[0];
        // }
        //结束
        c_newList.setData({
          BankAdvertisingData: result.data
        });

      },
      timeout: 10000,
      error: function () {
      }
    })   
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.getCarouselList()

    // 获取屏幕高度
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          clientHeight: res.windowHeight-60
        });
      }
    })
   

    this.update();
    var articleType = app.articleType;
    var that=this;
    var c_newList = this.selectComponent("#newList");
  
    /*设置默认值*/
    this.setData({
      "contact.Telephone":"0085328715699",
      "contact.MobilePhone":"0085365382047"
    });
    /*获取首页按揭文章*/
  /*  cache.get("allArticle", function (result) {
      console.log("开始获取");
      var allArticle = result;
     
      if (allArticle != undefined) {
        var filterArticle = dataFilter.getFilter("Type", "356", allArticle, 3);
        
        //console.log("筛选文章");
        //console.log(filterArticle);
        c_newList.setData({
          DataList: filterArticle
        });
      }
      else{
      
      }*/
    wx.request({
      // url: app.macauWebApiList.api_Article(app.macauweburl.webApiUrl,'70'),
      // url: "http://10.68.2.9/CenMacauCMS2/api/Article/GetList",
      url: app.macauweburl.webApiUrl+"Article/GetList",
      success: function (result) {
        //cache.clear();
        if (result == undefined && result == null) {
          return;
        }
        // cache.set("allArticle", result.data.InnerList);
        // mzh1804661
        var filterArticle = dataFilter.getFilter("Type", "EX", result.data.InnerList, 3);

        for (var i = 0; i < filterArticle.length;i++){
          filterArticle[i].Path = filterArticle[i].Images[0];
        }
        
        //结束
        c_newList.setData({
          DataList: filterArticle
        });


      },
      timeout: 10000,
      error: function () {

      }
    })    
      /*绑定点击事件*/
      c_newList.onItemTap(function (event) {
        
        app.console.log(event);
        var id=event.currentTarget.dataset.obj.Code;
  
        wx.navigateTo({
          url: '/pages/ArticleDetails/ArticleDetails?id='+id,
        });
        
      }); 

    //});

    /*联系我们*/
    wx.request({
      url: app.macauWebApiList.api_contact(app.macauweburl.webApiUrl),
      success: function (result) {
        // 替换
        var reg = new RegExp('http://www.centaline-macau.com', "g")
        result.data.HeadImg = result.data.HeadImg.replace(reg, "http://www.centaline-macao.com");
        that.setData({
          contact: result.data
        });
      }
    })
   

  },
  



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  /*强制更新 */
  update :function(){
    // 获取小程序更新机制兼容
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()

      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        console.log(res.hasUpdate)

      })
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已經準備好，是否馬上重啟小程序？',
              'confirmText': '確定',
              'cancelText': '取消',

              success: function (res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启

                  updateManager.applyUpdate();
                  wx.showLoading({
                    title: '正在重啟',
                  })
                }

              }
            })

          })
          updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
            wx.showModal({
              title: '已經有新版本',
              content: '新版本已經上線啦~，請你刪除當前小程序，重新搜索打開~',
            })
          })
        }
      })
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
      wx.showModal({
        title: '提示',
        content: '當前微信版本過低，無法使用該功能，請升級到最新微信版本后重試。'
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    telphoneCall 
  },
  telphoneCall:function(event){
    var tel = event.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  phoneCall:function(event){
    var phone = event.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    });
  }
  ,
  contactUs:function(){

    var contactAnimate=wx.createAnimation({
      duration: 200,
      timingFunction: "ease"
    });
    contactAnimate.translateY(-125).step();
    this.setData({
      contactShow: true,
      contactAnimate: contactAnimate
    });  
    //contactShow contact- content
  },
  closeContact:function(){
    var contactAnimate = wx.createAnimation({
      duration: 200,
      timingFunction: "ease"
    });
    contactAnimate.translateY(125).step();
    this.setData({
      contactShow: false,
      contactAnimate: contactAnimate
    });   
  }
})